﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace DeleteSDData
{
    class Program
    {
        static void Main(string[] args)
        {

            IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            IConfigurationRoot configuration = builder.Build();

            Console.Write("确定要删除sd卡上的照片文件吗??[Y/N] ");
            var result = Console.ReadLine();
            if (result.ToString().ToLower() == "n")
            {
                Console.WriteLine("停止处理!!");
                return;
            }
            Console.WriteLine("正在处理，请稍候！");
            var removableDiskNames = GetRemovableDeviceID();
            foreach (var diskName in removableDiskNames)
            {
                var isSonySDCard = Directory.Exists($"{diskName}AVF_INFO");
                if (isSonySDCard)
                {
                    Console.WriteLine("Sony SD 卡.");
                    Clear_Directory($"{diskName}AVF_INFO");
                }
                else
                {
                    Console.WriteLine("Limux SD 卡.");
                }
                if (Directory.Exists($"{diskName}DCIM"))
                {
                    Clear_Directory($"{diskName}DCIM");
                }
                if (isSonySDCard)
                {
                    CopyFolder(configuration.GetSection("AVF_INFO_Path").Value, $"{diskName}AVF_INFO");
                }
            }
            Console.WriteLine("处理完成!!");
        }

        static IEnumerable<string> GetRemovableDeviceID()
        {

            //return new string[] { @"I:\新建文件夹\" };
            ICollection<string> removableDiskNames = new List<string>();

            DriveInfo[] allDirves = DriveInfo.GetDrives();
            //检索计算机上的所有逻辑驱动器名称
            foreach (DriveInfo item in allDirves)
            {
                if (item.IsReady && item.DriveType == DriveType.Removable)
                {
                    removableDiskNames.Add(item.Name);
                }
            }
            return removableDiskNames;
        }

        static void Clear_Directory(string path)
        {
            if (Directory.Exists(path))
            {
                //获取该路径下的文件路径
                string[] filePathList = Directory.GetFiles(path);
                foreach (string filePath in filePathList)
                {
                    File.Delete(filePath);
                }

                //获取该路径下的文件夹路径
                string[] directorsList = Directory.GetDirectories(path);
                foreach (string directory in directorsList)
                {
                    Directory.Delete(directory, true);//删除该文件夹及该文件夹下包含的文件
                }
            }
        }

        /// <summary>
        /// 复制源文件夹下的所有内容到新文件夹
        /// </summary>
        /// <param name="sources">源文件夹路径</param>
        /// <param name="dest">新文件夹路径</param>
        static void CopyFolder(string sources, string dest)
        {
            if (!Directory.Exists(dest)) return;

            DirectoryInfo dinfo = new DirectoryInfo(sources);
            //注，这里面传的是路径，并不是文件，所以不能包含带后缀的文件                
            foreach (FileSystemInfo f in dinfo.GetFileSystemInfos())
            {
                //目标路径destName = 新文件夹路径 + 源文件夹下的子文件(或文件夹)名字                
                //Path.Combine(string a ,string b) 为合并两个字符串                     
                string destName = Path.Combine(dest, f.Name);
                if (f is FileInfo)
                {
                    //如果是文件就复制       
                    File.Copy(f.FullName, destName, true);//true代表可以覆盖同名文件                     
                }
                else
                {
                    //如果是文件夹就创建文件夹，然后递归复制              
                    Directory.CreateDirectory(destName);
                    CopyFolder(f.FullName, destName);
                }
            }
        }
    }
}
